class Task {
  constructor({ id, taskName, taskTime, done }) {
    this.id = id ? id : 'ID' + getHash(taskName + taskTime + done);
    this.taskName = taskName;
    this.taskTime = taskTime;
    this.done = done;
  }

  insertDOM() {
    const taskItems = document.querySelector('#taskItems');
    const taskItem = document.createElement('div');
    taskItem.classList.add('taskItem');
    if (this.done) taskItem.classList.add('del');
    taskItem.innerHTML = `
        <div class="del-line"></div>
        <input class="switchDone" type="checkbox">
        <input class="taskText" type="text">
        <input class="deadline" type="date">
        <button class="remove"><i class="far fa-trash-alt"></i></button>
  `;
    taskItem.getElementsByClassName('taskText')[0].value = this.taskName;
    taskItem.getElementsByClassName('deadline')[0].value = this.taskTime;
    taskItem.getElementsByClassName('switchDone')[0].checked = this.done;
    taskItem.id = this.id;
    taskItems.appendChild(taskItem);
    taskItem.getElementsByClassName('taskText')[0].disabled = this.done;
    taskItem.getElementsByClassName('deadline')[0].disabled = this.done;
  }

  objFilter() {
    return (({ id, taskName, taskTime, done }) => ({
      id,
      taskName,
      taskTime,
      done,
    }))(this);
  }

  insertLocalStorage() {
    let taskInfo = JSON.parse(localStorage.getItem('taskInfo'));
    taskInfo.push(this.objFilter());
    taskInfo = JSON.stringify(taskInfo);
    localStorage.setItem('taskInfo', taskInfo);
  }

  static removeLocalStorage(id) {
    console.log(id);
    let taskInfo = JSON.parse(localStorage.getItem('taskInfo'));
    const target = taskInfo.findIndex((d) => d.id == id);
    console.log(target)
    taskInfo.splice(target, 1);
    taskInfo = JSON.stringify(taskInfo);
    localStorage.setItem('taskInfo', taskInfo);
  }

  static updateLocalStorage(newInfo, id) {
    let taskInfo = JSON.parse(localStorage.getItem('taskInfo'));
    let targetIdx = taskInfo.findIndex((d) => {
      return d.id == id;
    });
    taskInfo[targetIdx] = { ...taskInfo[targetIdx], ...newInfo };
    taskInfo = JSON.stringify(taskInfo);
    localStorage.setItem('taskInfo', taskInfo);
  }
}

class TasksTool {
  constructor(arr = this.currentStorage) {
    if (arr instanceof Array) this.tks = arr;
    else {
      this.tks = JSON.parse(localStorage.getItem('taskInfo'));
    }
  }

  toTodays(cur = this.tks) {
    return new TasksTool(
      cur.filter(
        (d) =>
          new Date().setHours(0, 0, 0, 0) ===
          new Date(d.taskTime).setHours(0, 0, 0, 0)
      )
    );
  }

  toDone(cur = this.tks) {
    return new TasksTool(cur.filter((d) => d.done));
  }

  toPass(cur = this.tks) {
    return new TasksTool(
      cur.filter(
        (d) =>
          new Date().setHours(0, 0, 0, 0) >
          new Date(d.taskTime).setHours(0, 0, 0, 0)
      )
    );
  }

  resetAll() {
    const taskItems = document.getElementById('taskItems');
    while (taskItems.lastChild) {
      taskItems.removeChild(taskItems.lastChild);
    }
  }

  refreshAll(tasks = this.tks) {
    this.resetAll();
    tasks.forEach((d) => {
      const t = new Task(d);
      t.insertDOM();
    });
  }
}

getHash = (str) => {
  return str
    .split('')
    .reduce(
      (prevHash, currVal) =>
        ((prevHash << 5) - prevHash + currVal.charCodeAt(0)) | 0,
      0
    );
};
